﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SqlMiM.Common.Interfaces;
using SqlMiM.Common.Framework.Configuration;


namespace SqlMiM.SqlMirroring.Options
{
    public class MirroringOptionsPage : IOptionsPage
    {

        public const String NAME = "MirroringOptionsPage";

        // Default value fot theme
        public const String PRINCIPAL_MACHINENAME_DEFAULT_VALUE = ".";
        public const String PRINCIPAL_MACHINENAME_PROPERTY = "PrincipalMachineName";

        public const String MIRROR_MACHINENAME_DEFAULT_VALUE = ".";
        public const String MIRROR_MACHINENAME_PROPERTY = "MirrorMachineName";

        // Fields
        private MirroringOptions optionControl;
        private ViewModel optionViewModel;

        private ConfigurationObject configurationObject;

        // Methods
        public MirroringOptionsPage()
        {
        }

        public void Cancel()
        {
            if (this.optionViewModel != null)
            {
                //this.shellOptionsModel.Cancel();
                this.optionViewModel = null;
                this.optionControl = null;
            }
        }

        public void Commit()
        {
            if (this.optionControl == null)
                return;

            //byte newAvgPercentFragmentation = this.optionViewModel.AvgPercentFragmentation;
            //byte actualAvgPercentFragmentation = (Byte)this.configurationObject.GetProperty(MirroringOptionsPage.AVG_PCT_FRAG_PROPERTY, MirroringOptionsPage.AVG_PCT_FRAG_DEFAULT_VALUE);

            //if (newAvgPercentFragmentation != actualAvgPercentFragmentation)
            //    this.configurationObject.SetProperty(MirroringOptionsPage.AVG_PCT_FRAG_PROPERTY, newAvgPercentFragmentation);

            //Boolean newGetSystemTables = this.optionViewModel.IsSystemDatabasesEnabled;
            //Boolean actualGetSystemTables = (Boolean)this.configurationObject.GetProperty(MirroringOptionsPage.GET_SYSTEM_TABLES_PROPERTY, MirroringOptionsPage.GET_SYSTEM_TABLES_DEFAULT_VALUE);

            //if (newGetSystemTables != actualGetSystemTables)
            //    this.configurationObject.SetProperty(MirroringOptionsPage.GET_SYSTEM_TABLES_PROPERTY, newGetSystemTables);

            //Int32 newIndexCount = this.optionViewModel.MaxIndexes;
            //Int32 actualIndexCount = (Int32)this.configurationObject.GetProperty(MirroringOptionsPage.MAX_INDEX_COUNT_PROPERTY, MirroringOptionsPage.MAX_INDEX_COUNT_DEFAULT_VALUE);

            //if (newIndexCount != actualIndexCount)
            //    this.configurationObject.SetProperty(MirroringOptionsPage.MAX_INDEX_COUNT_PROPERTY, newIndexCount);

            this.optionViewModel = null;
            this.optionControl = null;

        }

        public void Load(ConfigurationObject value)
        {
            this.configurationObject = value;
        }

        // Properties
        public object Content
        {
            get
            {
                if (this.optionControl == null)
                {
                    this.optionControl = new MirroringOptions();
                    this.optionViewModel = new ViewModel();

                    //byte actualAvgPercentFragmentation = (Byte)this.configurationObject.GetProperty(MirroringOptionsPage.AVG_PCT_FRAG_PROPERTY, MirroringOptionsPage.AVG_PCT_FRAG_DEFAULT_VALUE);
                    //Boolean actualGetSystemTables = (Boolean)this.configurationObject.GetProperty(MirroringOptionsPage.GET_SYSTEM_TABLES_PROPERTY, MirroringOptionsPage.GET_SYSTEM_TABLES_DEFAULT_VALUE);
                    //Int32 actualIndexCount = (Int32)this.configurationObject.GetProperty(MirroringOptionsPage.MAX_INDEX_COUNT_PROPERTY, MirroringOptionsPage.MAX_INDEX_COUNT_DEFAULT_VALUE);
 
                    //this.optionViewModel.AvgPercentFragmentation = actualAvgPercentFragmentation;
                    //this.optionViewModel.IsSystemDatabasesEnabled = actualGetSystemTables;
                    //this.optionViewModel.MaxIndexes = actualIndexCount;

                    this.optionControl.DataContext = this.optionViewModel;

                }

                return this.optionControl;
            }
        }

        public string Name
        {
            get
            {
                return "Mirroring";
            }
        }

        public string Title
        {
            get
            {
                return "Mirroring";
            }
        }
    }

}
